package com.yzt.message.custom;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yzt.zhuju.network.entity.message.GiftInfoBean;
import com.yzt.plugin_imageloader.ImageLoadUtil;
import com.yzt.message.R;
import com.yzt.zhuju.RXingUtils;

import java.util.List;

import io.rong.imkit.conversation.extension.component.emoticon.AndroidEmoji;
import io.rong.imkit.model.UiMessage;
import io.rong.imkit.widget.adapter.IViewProviderListener;
import io.rong.imlib.model.MessageContent;

/**
 * 礼物的容器
 */
public class RXGiftMessageItemProvider extends CustomBaseMessageItemProvider<RXGiftMessageContent> {

    public RXGiftMessageItemProvider() {
        mConfig.showReadState = true;
        mConfig.showContentBubble = false;
    }

    @Override
    protected ViewHolder onCreateMessageContentViewHolder(ViewGroup parent, int i) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.nomi_rc_item_gift_message, parent, false);
        return new ViewHolder(view.getContext(), view);
    }

    @Override
    protected void bindMessageContentViewHolder(io.rong.imkit.widget.adapter.ViewHolder viewHolder,
                                                io.rong.imkit.widget.adapter.ViewHolder viewHolder1,
                                                RXGiftMessageContent content, UiMessage data,
                                                int position, List<UiMessage> list,
                                                IViewProviderListener<UiMessage> iViewProviderListener) {
        boolean self = RXingUtils.getUserId().equals(data.getMessage().getTargetId());
        ViewHolder holder = (ViewHolder) viewHolder;
        GiftInfoBean extra = content.getMessageExtra();
        if (extra != null) {
            ImageLoadUtil.loadIv(holder.img_gift.getContext(), extra.getGiftIcon(), holder.img_gift);
            holder.tv_gift_name.setText("·赠送 " + extra.getGiftName() + "·");
            if (self) {
                holder.tv_content.setText("送礼物「" + extra.getGiftName() + "」给你");
            } else {
                holder.tv_content.setText("送礼物「" + extra.getGiftName() + "」");
            }
        }
    }

    @Override
    protected boolean onItemClick(io.rong.imkit.widget.adapter.ViewHolder viewHolder, RXGiftMessageContent content, UiMessage message, int position, List<UiMessage> list, IViewProviderListener<UiMessage> iViewProviderListener) {
        return false;
    }

    @Override
    protected boolean isMessageViewType(MessageContent messageContent) {
        return messageContent instanceof RXGiftMessageContent && !messageContent.isDestruct();
    }

    @Override
    public Spannable getSummarySpannable(Context context, RXGiftMessageContent data) {
        if (data == null) {
            return null;
        }
        String content = data.getContent();
        if (content != null) {
            if (content.length() > 100) {
                content = content.substring(0, 100);
            }
            return new SpannableString(AndroidEmoji.ensure(content));
        }
        return null;
    }

    private static class ViewHolder extends io.rong.imkit.widget.adapter.ViewHolder {

        ImageView img_gift;
        TextView tv_gift_name;
        TextView tv_content;

        public ViewHolder(Context context, View view) {
            super(context, view);
            this.img_gift = view.findViewById(R.id.img_gift);
            this.tv_gift_name = view.findViewById(R.id.tv_gift_name);
            this.tv_content = view.findViewById(R.id.tv_content);
        }
    }
}